//
//  MPTLiveOverView.m
//  MPTLive
//
//  Created by caixun on 15/8/20.
//  Copyright (c) 2015年 Jeakin. All rights reserved.
//

#import "MPTLiveOverView.h"
#import "UIColor+MPTColor.h"
#import "MPTLikeView.h"
#import "UIFont+MPTFont.h"
#import "Masonry.h"
#import "MPTLiveCustomLabel.h"
#import "MPTTool.h"

@interface MPTLiveOverView ()

@property (nonatomic, strong) UILabel *labTitle;
@property (nonatomic, strong) UILabel *labState;
@property (nonatomic, strong) UIButton *goonLiveBtn;
@property (nonatomic, strong) UIButton *goMyHomeBtn;
@property (nonatomic, strong) UIButton *closeBtn;
@property (nonatomic, strong) MPTLiveCustomLabel *userNum;
@property (nonatomic, strong) MPTLiveCustomLabel *likeNum;


@end


@implementation MPTLiveOverView

#pragma mark - ***************************** View Lifecycle ***********************************

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self initMainViews];
    }
    return self;
}

-(void)willMoveToSuperview:(UIView *)newSuperview
{
    [super willMoveToSuperview:newSuperview];
    
    if(newSuperview == nil)
    {
        [[self subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
        
        return ;
    }
}


#pragma mark - ******************************** Init All Views **********************************

- (void)initMainViews
{
    // 半透的遮罩
    UIImageView *bgImageView = [[UIImageView alloc] initWithFrame:self.frame];
    bgImageView.backgroundColor = [UIColor blackColor];
    bgImageView.alpha = 0.5;
    [self addSubview:bgImageView];
    
    CGRect titleRect = CGRectMake(0,screenH*0.25,screenW,30);
    self.labTitle = [[UILabel alloc] initWithFrame:titleRect];
    self.labTitle.text = @"辛苦了!";
    self.labTitle.font = [UIFont YXFontOfSize:24.0f];
    self.labTitle.textAlignment = NSTextAlignmentCenter;
    self.labTitle.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5f];
    self.labTitle.backgroundColor = [UIColor clearColor];
    [self addSubview:self.labTitle];
    
    CGRect stateRect = CGRectMake(0,
                                  self.labTitle.frame.origin.y+self.labTitle.frame.size.height+19,
                                  screenW,
                                  30);
    self.labState = [[UILabel alloc] initWithFrame:stateRect];
    self.labState.text = @"直播已结束";
    self.labState.font = [UIFont YXFontOfSize:24.0f];
    self.labState.textAlignment = NSTextAlignmentCenter;
    self.labState.textColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.5f];
    self.labState.backgroundColor = [UIColor clearColor];
    [self addSubview:self.labState];
    
 
    // 观众数量
    self.userNum = [[MPTLiveCustomLabel alloc] init];
    self.userNum.frame = CGRectMake(0, self.labState.frame.origin.y+self.labState.frame.size.height+77, screenW/2-30, 26);
    self.userNum.textAlignment = NSTextAlignmentRight;
    [self addSubview:self.userNum];
    
    // 点赞数量
    self.likeNum = [[MPTLiveCustomLabel alloc] init];
    self.likeNum.frame = CGRectMake(self.userNum.frame.size.width+30+30, self.userNum.frame.origin.y, screenW/2, 26);
    self.likeNum.textAlignment = NSTextAlignmentLeft;
    [self addSubview:self.likeNum];
    
    
    
    CGRect goonLiveRect = CGRectMake(20,
                                     screenH-20-44,
                                     screenW-20-20,
                                     44);
    self.goonLiveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.goonLiveBtn.frame = goonLiveRect;
    [self.goonLiveBtn setTitle:@"继续直播" forState:UIControlStateNormal];
    [self.goonLiveBtn setBackgroundColor:[UIColor YXColorWithHexCode:@"#ffe200"]];
    [self.goonLiveBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.goonLiveBtn.layer.cornerRadius = 22;
    self.goonLiveBtn.titleLabel.font = [UIFont YXFontOfSize:16.0f];
    self.goonLiveBtn.clipsToBounds = YES;
    [self addSubview:self.goonLiveBtn];
    
    //
    UIImage *close = [UIImage imageNamed:@"LiveOverClose"];
    UIImage *closePress = [UIImage imageNamed:@"LiveOverClosePress"];
    CGRect closeBtnRect = CGRectMake(screenW-close.size.width-30,
                                     10,
                                     close.size.width+30,
                                     close.size.height+30);
    self.closeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.closeBtn.frame = closeBtnRect;
    [self.closeBtn setImage:close forState:UIControlStateNormal];
    [self.closeBtn setImage:closePress forState:UIControlStateHighlighted];
    [self addSubview:self.closeBtn];
}


#pragma mark - ****************************** Functions *********************************

- (void)setTargetForBtn:(id)target
     withGoonLiveAction:(SEL)goonLiveAction
     withgoMyHomeAction:(SEL)goMyHomeAction
        withCloseAction:(SEL)closeAction
{
    [self.goMyHomeBtn addTarget:target action:goMyHomeAction forControlEvents:UIControlEventTouchUpInside];
    [self.goonLiveBtn addTarget:target action:goonLiveAction forControlEvents:UIControlEventTouchUpInside];
    [self.closeBtn addTarget:target action:closeAction forControlEvents:UIControlEventTouchUpInside];
}

- (void)setViewContent:(NSString *)likeNum withMemberNum:(NSString *)memberNum
{
    self.likeNum.text = [NSString stringWithFormat:@"%@ 赞",[MPTTool getNumWith:likeNum.integerValue]];
    [self.likeNum setFont:[UIFont YXFontOfSize:18.0f]
                fromIndex:0
                   length:self.likeNum.text.length];
    [self.likeNum setFont:[UIFont YXFontOfSize:21.0f]
                fromIndex:0
                   length:likeNum.length];
    [self.likeNum setColor:[UIColor YXColorWithHexCode:@"#cfcfcf"] fromIndex:0 length:self.likeNum.text.length];
    
    
    self.userNum.text = [NSString stringWithFormat:@"%@ 观众",[MPTTool getNumWith:memberNum.integerValue]];
    [self.userNum setFont:[UIFont YXFontOfSize:18.0f]
                fromIndex:0
                   length:self.userNum.text.length];
    [self.userNum setFont:[UIFont YXFontOfSize:21.0f]
                fromIndex:0
                   length:memberNum.length];
    [self.userNum setColor:[UIColor YXColorWithHexCode:@"#cfcfcf"] fromIndex:0 length:self.userNum.text.length];
}


@end
